package homework.question5;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class RestDayCalculation {
    private static final LocalDate FIRST_REST_DAY = LocalDate.of(2020, 2, 1);

    public static void main(String[] args) {
        //目标:掌握JDK8时间类的使用
        //进行方法调用
        restDayCalc();

    }

    //休息日的计算(独立功能独立成方法)
    public static void restDayCalc() {
        /*//创建扫描器
        Scanner sc = new Scanner(System.in);
        //提示用户输入
        System.out.println("请输入年份和月份,注意格式:xxxx-xx");
        String time = sc.next();

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        //创建LocalDate对象
        LocalDate localDate = LocalDate.parse(time+ "-01", dtf);
        //输出当前月份的月末
        System.out.println(localDate.getDayOfMonth());
        System.out.println(localDate);
*/
        try (Scanner sc = new Scanner(System.in)) {
            System.out.println("请输入年份和月份（格式：yyyy-MM）：");
            String input = sc.next();

            // 解析输入日期
            YearMonth yearMonth = YearMonth.parse(input, DateTimeFormatter.ofPattern("yyyy-MM"));
            LocalDate startDate = yearMonth.atDay(1);
            LocalDate endDate = yearMonth.atEndOfMonth();

            // 计算所有休息日
            List<LocalDate> restDays = calculateRestDays(startDate, endDate);

            // 统计结果
            int totalRestDays = restDays.size();
            long weekendRestDays = restDays.stream()
                    .filter(date -> date.getDayOfWeek() == DayOfWeek.SATURDAY ||
                            date.getDayOfWeek() == DayOfWeek.SUNDAY)
                    .count();

            // 打印日历
            printCalendar(yearMonth, restDays);

            // 输出统计信息
            System.out.printf("\n本月休息天数：%d天\n", totalRestDays);
            System.out.printf("周末休息天数：%d天\n", weekendRestDays);

        } catch (Exception e) {
            System.err.println("输入格式错误，请使用yyyy-MM格式（例如：2023-05）");
        }
    }

    private static List<LocalDate> calculateRestDays(LocalDate startDate, LocalDate endDate) {
        List<LocalDate> restDays = new ArrayList<>();

        // 计算从首次休息日到查询月份开始前的天数
        long daysBetween = java.time.temporal.ChronoUnit.DAYS.between(FIRST_REST_DAY, startDate);

        // 找到查询月份前的最后一个休息日
        LocalDate lastRestBeforeMonth = FIRST_REST_DAY.plusDays((daysBetween / 4) * 4);

        // 从最后一个休息日开始计算本月的休息日
        LocalDate currentRestDay = lastRestBeforeMonth;
        while (currentRestDay.isBefore(startDate)) {
            currentRestDay = currentRestDay.plusDays(4);
        }

        // 收集本月所有休息日
        while (!currentRestDay.isAfter(endDate)) {
            restDays.add(currentRestDay);
            currentRestDay = currentRestDay.plusDays(4);
        }

        return restDays;
    }

    private static void printCalendar(YearMonth yearMonth, List<LocalDate> restDays) {
        LocalDate date = yearMonth.atDay(1);
        int maxDay = yearMonth.lengthOfMonth();
        DayOfWeek firstDayOfWeek = date.getDayOfWeek();

        System.out.println("\n" + yearMonth.getMonth() + " " + yearMonth.getYear());
        System.out.println("日 一 二 三 四 五 六");

        // 首行缩进
        for (int i = 0; i < firstDayOfWeek.getValue() % 7; i++) {
            System.out.print("   ");
        }

        // 打印日历
        for (int day = 1; day <= maxDay; day++) {
            LocalDate currentDate = yearMonth.atDay(day);
            if (restDays.contains(currentDate)) {
                System.out.printf("[%2d]", day); // 休息日用中括号标记
            } else {
                System.out.printf("%3d", day);
            }

            // 换行判断
            if (currentDate.getDayOfWeek() == DayOfWeek.SATURDAY) {
                System.out.println();
            }
        }
        System.out.println();
    }
}

